/**
 * @Author              : wanghw
 * @Date                : 2019/6/13 - 13:15
 * @PROJECT_NAME        : Examination
 * @FileName            : test
 * @Last Modified       : 2019/10/21 - 16:16 - wanghw
 * @describe            ：过滤器封装
 * @Example             :
 */
export default {
    currency(v, moneyType = '¥') {
        v = parseFloat(v).toFixed(2)
        return moneyType + v
    },
    date(v) { // 时间戳
        let date = new Date(v);
        let dateN = ''
        try {
            dateN = date.getFullYear() + '-' +
                (date.getMonth() + 1).toString().padStart(2, '0') + '-' +
                (date.getDate()).toString().padStart(2, '0') + ' ' +
                (date.getHours()).toString().padStart(2, '0') + ':' +
                (date.getMinutes()).toString().padStart(2, '0') + ':' +
                (date.getSeconds()).toString().padStart(2, '0');
        } catch (e) {
            dateN = date.getFullYear() + '-' +
                (date.getMonth() + 1).toString() + '-' +
                (date.getDate()).toString() + ' ' +
                (date.getHours()).toString() + ':' +
                (date.getMinutes()).toString() + ':' +
                (date.getSeconds()).toString();
        }
        return dateN
    },
    getImgUrl(imgName) {
        return 'http://127.0.0.1/' + imgName;
    },
    startEndDate(v) { // 时间戳
        let vdate = v.replace(/\//g, '').split('(')[1].split(')')[0] / 1
        let date = new Date(vdate);
        return date.getFullYear() +
            '.' + (date.getMonth() + 1).toString() +
            '.' + (date.getDate()).toString()
    },
    // 秒数转时间格式
    formatSecondsFilter(value, type) {
        value = parseInt(value)
        let secondTime = parseInt(value) // 秒
        let minuteTime = 0 // 分
        let hourTime = 0 // 小时
        let dayTime = 0 // 天
        let result = '' // 返回结果
        let sStr // 秒拼接字符串
        let mStr // 分拼接字符串
        let hStr // 时拼接字符串
        let dStr // 天拼接字符串
        let typeDictionary = [ // 输出类型配置字典
            { type: 'china', list: ['天', '时', '分', '秒'] }, // 文字单位
            { type: 'chinaNoSecond', list: ['天', '时', '分', ''] }, // 文字单位无秒数
            { type: 'alphabet', list: ['d', 'h', 'm', 's'] }, // 字母单位
            { type: 'alphabetNoSecond', list: ['d', 'h', 'm', ''] }, // 字母单位无秒数
            { type: 'colon', list: ['', ':', ':', ''] }, // 冒号单位
            { type: 'quotes', list: ['', ':', '\'', '"'] } // 引号单位
        ]
        let tempIndex = typeDictionary.findIndex(item => item.type === type)
        tempIndex = tempIndex === -1 ? 0 : tempIndex // 默认使用字典第一条
        if (value < 0) {
            return
        } else if (value < 60) {
            secondTime = value
        } else if (value < 3600) {
            secondTime = value % 60
            minuteTime = Math.floor(value / 60)
        } else if (value < 3600 * 24) {
            secondTime = value % 60
            minuteTime = Math.floor(value / 60) % 60
            hourTime = Math.floor(value / (60 * 60))
        } else {
            secondTime = value % 60
            minuteTime = Math.floor(value / 60) % 60
            hourTime = Math.floor(value / (60 * 60)) % 24
            dayTime = Math.floor(value / (60 * 60 * 24))
        }
        secondTime = secondTime > 9 ? secondTime : secondTime > 0 ? '0' + secondTime : 0
        minuteTime = minuteTime > 9 ? minuteTime : minuteTime > 0 ? '0' + minuteTime : 0
        hourTime = hourTime > 0 ? hourTime : 0
        dayTime = dayTime > 0 ? dayTime : 0

        sStr = secondTime !== 0 ? secondTime + typeDictionary[tempIndex].list[3] : ''
        mStr = minuteTime !== 0 ? minuteTime + typeDictionary[tempIndex].list[2] : ''
        hStr = hourTime !== 0 ? hourTime + typeDictionary[tempIndex].list[1] : ''
        dStr = dayTime !== 0 ? dayTime + typeDictionary[tempIndex].list[0] : ''

        if (value === 0) {
            sStr = '0' + typeDictionary[tempIndex].list[3]
        }

        switch (type) {
            case 'colon':
                sStr = secondTime === 0 ? '00' : sStr
                mStr = minuteTime === 0 ? '00' + typeDictionary[tempIndex].list[2] : mStr
                hStr = (hourTime !== 0 || dayTime !== 0) ? (parseInt(hourTime) + parseInt(dayTime) * 24) + typeDictionary[tempIndex].list[1] : ''
                dStr = ''
                break;
            case 'quotes':
                hStr = (hourTime !== 0 || dayTime !== 0) ? (parseInt(hourTime) + parseInt(dayTime) * 24) + typeDictionary[tempIndex].list[1] : ''
                dStr = ''
                break;
            case 'chinaNoSecond':
                sStr = ''
                break;
            case 'alphabetNoSecond':
                sStr = ''
                break;
            default:
                break;
        }
        result = dStr + hStr + mStr + sStr
        return result
    },
    getTopicTypes(type) {
        if (type == '1') {
            return '单选';
        } else if (type == '2') {
            return '多选'
        } else if (type == '4') {
            return '填空'
        } else if (type == '5') {
            return '判断'
        } else if (type == '3') {
            return '问答'
        }
    }
}
